import axios from "axios";
import storage from "../../storage/storage";
import { ElMessage } from 'element-plus'
axios.defaults.headers["Content-Type"] = "application/json;charset=utf-8";

const http = axios.create({
  baseURL: import.meta.env.VITE_BASEURL,
  timeout: 5000,
});

//请求拦截器
http.interceptors.request.use(
  (res) => {
    res.headers["session"] = storage.getCookies("session");
    return res;
  },
  (error) => {
    return Promise.reject(error);
  }
);

//响应拦截器
http.interceptors.response.use(
  (res) => {
    if (res.status == 200) {
      if (res.data.code == 200) {
        return Promise.resolve(res.data);
      } else {
        ElMessage.error(res.data.msg);
        return Promise.reject(res.data);
      }
    } else {
      return Promise.reject(res);
    }
  },
  (err) => {
    return Promise.reject(err);
  }
);

export default http;
